package com.liaoyoule.multiple.exceptionAdvice;

import com.alibaba.fastjson.JSONObject;
import com.liaoyoule.multiple.exception.NotFoundParameterizedException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;

/**
 * Created by 40864 on 2017/2/20.
 */
@ControllerAdvice
public class GlobalDefaultExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public void defaultErrorHandler(HttpServletRequest request, PrintWriter printWriter, Exception e)  {
        //打印异常信息：
        e.printStackTrace();
        System.out.println("GlobalDefaultExceptionHandler.defaultErrorHandler()");
        if (e instanceof IncorrectCredentialsException){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("403", "密码不匹配");
            printWriter.write(jsonObject.toJSONString());
        }


    }


    @ExceptionHandler(value = NotFoundParameterizedException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ResponseEntity defaultErrorHandler(NotFoundParameterizedException e)  {
        return new ResponseEntity(e.getErrorVM(),HttpStatus.NOT_FOUND);
    }

}
